package cn.hicard.risk.dao.bd.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.core.Where;
import cn.hicard.risk.dao.BaseDao;
import cn.hicard.risk.dao.bd.MerchInfoDao;
import cn.hicard.risk.entity.BdMerchinfo;

@Repository
public class MerchInfoDaoImpl extends BaseDao<BdMerchinfo> implements MerchInfoDao{

	/**
	 * 查询商户资料List
	 */
	public PageResult <Map<String, Object>>getMerchList(Where where, Page page)
			throws Exception {
		String listSql = "SELECT  m.pk_hmerch,m.hm_id,b.ht_id,b.om_id,b.ot_id,m.hm_name,m.status,b.aegis_company  " +
				",m.industry,m.cellphone,m.address,m.telphone,m.print_name,m.print_no,m.type,m.merch_degree,m.status" +
				",m.suspend_remit,m.risk_degree,m.loginid,m.update_time,m.dayliquidation,m.monthliquidation,m.dayclearinglimits,m.monthclearinglimits" +
				",m.get_ticket,m.contact_name,m.hm_name_short,m.expand_people,m.partner,m.the_province,m.the_city FROM bd_merchinfo m LEFT JOIN bd_business b on  m.hm_id=b.hm_id   WHERE 1=1 " + where.queryString()+" group by b.ht_id,b.om_id,b.ot_id,m.hm_id order by pk_hmerch";
		String countSql = "select count(s.pk_hmerch) from (SELECT  m.pk_hmerch FROM bd_merchinfo m LEFT JOIN bd_business b on  m.hm_id=b.hm_id where 1=1 " + where.queryString()+"  group by b.ht_id,b.om_id,b.ot_id,m.hm_id) s";		
		PageResult <Map<String, Object>>pr = new PageResult();
		pr.setDataList(super.findMapBySqlPage(listSql, page, where.getParams()));
		pr.setResultCount(super.findCountBySql(countSql, where.getParams()));
		return pr;
	}
	

	
	@Override
	public List<BdMerchinfo> findList(Where where) throws Exception {
		String hql = "FROM BdMerchinfo WHERE 1=1 "+where.queryString();
		return super.findByHql(hql, where.getParams());
	}
	
	/**
	 * 黑名单，灰名单报表下载
	 * 汇卡商户号,汇卡名字,更新时间,原因描述,风控等级
	 */
	public List<Object[]> getMerchReportList(Where where) throws Exception{
		String sql="select hm_id,hm_name,update_time,'',risk_degree from bd_merchinfo WHERE 1=1 "+where.queryString();
		return super.findFieldsBySql(sql, where.getParams());
	}
	
	public void save(BdMerchinfo bdMerchinfo) throws Exception{
		super.save(bdMerchinfo);
	}
	
	public void update(BdMerchinfo bdMerchinfo) throws Exception{
		super.update(bdMerchinfo);
	}
	
	public void getOne(int id) throws Exception{
		super.getByPk(id);
	}
	
    public List<BdMerchinfo> findLimitMerch()throws Exception {
		
		String hql = "FROM BdMerchinfo WHERE (dayliquidation<>NULL AND dayliquidation <>'') OR (dayclearinglimits<>NULL AND dayclearinglimits<>'') ";
		List<BdMerchinfo> list = super.findByHql(hql);
		return list;
	}
    
   public BdMerchinfo getOne(BdMerchinfo merch) throws Exception {
		
		String hkMerchno = merch.getHmId();
		BdMerchinfo result = null;
	
		// 其次使用应该是唯一的[汇卡商户号]查询
		if(hkMerchno != null){
			List<BdMerchinfo> list = super.findByHql("FROM BdMerchinfo WHERE hmId=?", hkMerchno);
			if(list !=null && list.size() > 0){
				result = list.get(0);
			}
		}
		return result;
	}
}
